package com.sz.app.oss.api.common.sup;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.sup.entity.SupUser;
import com.sz.biz.common.sup.service.SupplierUserService;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.BizException;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Function: TODO: ADD FUNCTION <br>
 * Author: Johnboy <br>
 * Date: 2017-10-12 20:41:00
 */
@RestController
@RequestMapping("${api}/sup/suppliers/user")
@Api(description = " ", tags = "021-003、供应商管理-用户管理")
public class SupplierUserController extends BaseController{

    @Autowired
    private SupplierUserService supplierUserService;

    /**
     * 获取用户账号列表
     *
     * @throws BizException ex
     */
    @ApiOperation(value = "搜索客户账号", notes = "搜索用户，返回用户列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字",
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortName", value = "查询的关键字",
                    dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "isAsc", value = "排序类型,true为升序，false为降序", required = false,
                    dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页 默认第一页",
                    dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小",
                    dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "refSupplier", value = "供应商客户关联id",
                    dataType = "int", paramType = "query")})
    @RequestMapping(value = "/search", method = {RequestMethod.GET})
    public QResultDto getUser(@RequestParam(defaultValue = "") String q,
                              @RequestParam(defaultValue = "userName") String sortName,
                              @RequestParam(required = false, defaultValue = "true") boolean isAsc,
                              @RequestParam(defaultValue = "1") int pageIndex,
                              @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                              @RequestParam int refSupplier) {
        return supplierUserService.find(q, pageIndex, pageSize, sortName, isAsc, refSupplier);
    }

    @ApiOperation(value = "新建供应商用户", notes = "新建供应商用户"+Permissions.CUSTOMER_LOGISTICS_MGT + " or " +Permissions.CUSTOMER_TRADE_MGT)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS, Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE},
            logical = Logical.OR)
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultDto save(@RequestBody SupUser supUser) {
        int sid = supplierUserService.save(supUser);
        ResultDto resultDto = getSaveSuccessDto(MessagesCodes.MODULE_SUPPLIER_USER);
        resultDto.setData(sid);
        return resultDto;
    }

    /**
     * 修改一个账号
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "修改供应商账号", notes = "修改供应商账号" +
            Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS + " or " +Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS,Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE},
            logical = Logical.OR)
    @RequestMapping(value = "", method = {RequestMethod.PUT})
    public ResultDto updateUser(@RequestBody @Validated SupUser supUser){
        List<SupUser> supUsers = new ArrayList<>();
        supUsers.add(supUser);
        supplierUserService.updateSupplierUser(supUsers);
        ResultDto dto = getUpdateSuccessDto(MessagesCodes.MODULE_SUPPLIER_SUBACCOUNT);
        dto.setData(supUser.getId());
        return dto;
    }

    /**
     * 锁定客户 不允许登录
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "锁定客户", notes = "不允许登录<br>权限：" +
            Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS + " or " +Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS,Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE},
            logical = Logical.OR)
    @RequestMapping(value = "/lock", method = {RequestMethod.POST})
    public ResultDto lockCustomer(@ApiParam(value = "客户的ids", required = true) @RequestBody List<Integer> ids) {
        supplierUserService.lockSupplierUser(ids);
        ResultDto dto = getOperateSuccessDto(MessagesCodes.MODULE_SUPPLIER_LOCK_SUCCESS);
        dto.setData(ids);
        return dto;
    }

    /**
     * 解锁客户子账户
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "解锁定客户", notes = "允许登录<br>权限：" +
            Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS + " or " +Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS,Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE},
            logical = Logical.OR)
    @RequestMapping(value = "/unlock", method = {RequestMethod.POST})
    public ResultDto unLockUser(@RequestBody List<Integer> ids) throws Exception {
        supplierUserService.unlockSupplierUser(ids);
        ResultDto dto = getOperateSuccessDto(MessagesCodes.MODULE_SUPPLIER_UNLOCK_SUCCESS);
        dto.setData(ids);
        return dto;
    }

    /**
     * 批量删除客户 非物理删除 打标志 不允许登录
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "批量删除客户", notes = "非物理删除 打标志 不允许登录<br>权限：" +
            Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS + " or " +Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "客户的ids",
                    required = true, dataType = "string", paramType = "body")})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS,Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE},
            logical = Logical.OR)
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    public ResultDto deleteCustomer(@RequestBody List<Integer> ids) throws Exception {
        supplierUserService.batchDelete(ids);
        return getDeleteSuccessDto(MessagesCodes.MODULE_SUPPLIER_MAINACCOUNT);
    }

    /**
     * 删除一个账号 非物理删除 打标志 不允许登录
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "删除一个客户账号", notes = "非物理删除 打标志 不允许登录<br>权限："  +
            Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS + " or " +Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "客户账号的id ", required = true, dataType = "int", paramType = "path")})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS,Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE},
            logical = Logical.OR)
    @RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
    public ResultDto deleteUser(@PathVariable int id){
        supplierUserService.batchDelete(Arrays.asList(id));
        ResultDto dto = getDeleteSuccessDto(MessagesCodes.MODULE_SUPPLIER_SUBACCOUNT);
        dto.setData(id);
        return dto;
    }

    /**
     * 重置客户账号密码
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "重置客户账号密码", notes = "-<br>权限："  +
            Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS + " or " +Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS,Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE},
            logical = Logical.OR)
    @RequestMapping(value = "/resetpasswd", method = {RequestMethod.POST})
    public ResultDto resetpasswd(
            @ApiParam(value = "账号ids", required = true) @RequestBody List<Integer> userids) throws Exception {
        supplierUserService.resetUsersPasswd(userids);
        ResultDto dto = getOperateSuccessDto(MessagesCodes.USER_RESET_PASSWORD_SUCCESS);
        dto.setData(userids);
        return dto;
    }

    /**
     * 改变客户账号密码
     */
    @ApiOperation(value = "更改客户账号密码", notes = "-<br>权限："  +
            Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS + " or " +Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "客户账号的id ",
                    required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码",
                    required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "newPassword", value = "新密码",
                    required = true, dataType = "string", paramType = "query")})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS,Permissions.BIZ_MGT_BASE_SUPPLIER_TRADE},
            logical = Logical.OR)
    @RequestMapping(value = "/updatepassword/{id}", method = {RequestMethod.PUT})
    public ResultDto changePassword(@PathVariable int id,
                                    @RequestParam String oldPassword,
                                    @RequestParam String newPassword) {
        supplierUserService.changePassword(id, oldPassword, newPassword);
        return new ResultDto();
    }

}
